package com.vinyl.android.chinese.fragment;

import java.util.ArrayList;

import com.vinyl.android.chinese.adapter.DicDbAdapter;
import com.vinyl.android.chinese.adapter.DicExpandableAdapter;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ListView;
 
public class InfoFragment extends BaseExpandableListFragment {
	private DicDbAdapter mDicAdapter;
	private String mCategory;
	private ArrayList<String> mGroup;
	private ArrayList<ArrayList<ArrayList<String>>> mChilds;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		

		mDicAdapter = new DicDbAdapter(getActivity().getApplicationContext());
		mDicAdapter.open();
	
		Intent intent = getActivity().getIntent();
		mCategory = intent.getExtras().getString("category");
		
		Log.d("TourtipFragment", "###Category: " + mCategory);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		mDicAdapter.close();
	}

	@Override
	public ExpandableListAdapter getDicAdapter() {

		mGroup = new ArrayList<String>();
		mChilds = new ArrayList<ArrayList<ArrayList<String>>>();
		
		Cursor result = mDicAdapter.fetchCategory("1");
		int i = 0;
		while(!result.isLast()) {
			mGroup.add(result.getString(2));
			
			mChilds.add(new ArrayList<ArrayList<String>>());
			mChilds.get(i).add(new ArrayList<String>());
			mChilds.get(i).get(0).add(result.getString(3));
			mChilds.get(i).get(0).add(result.getString(4));
			mChilds.get(i).get(0).add(result.getString(5));			
			i++;
			result.moveToNext();
		}
		result.close();
		
		DicExpandableAdapter adapter = new DicExpandableAdapter(getActivity(), mGroup, mChilds);
		
		return adapter;
	}
}